//
//  SPLiveHistoryTableViewController.m
//  panzer
//
//  Created by 还带大道 on 2018/10/21.
//  Copyright © 2018年 还带大道. All rights reserved.
//

#import "SPLiveHistoryTableViewController.h"
#import "SPCommon.h"
#import "SPLiveHistoryTableViewCell.h"
#import "SPLiveDeatilTableViewController.h"
#import "SPHistoryLiveModel.h"

@interface SPLiveHistoryTableViewController ()
@property (nonatomic,assign) NSInteger pageIndex;
@property (nonatomic,strong) NSMutableArray<SPHistoryLiveModel *> *dataSource;

@end

@implementation SPLiveHistoryTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"历史直播";
    self.pageIndex = 1;
    _dataSource = [NSMutableArray array];
    [self setupTableView];
    [self fetchData];
}


- (void)setupTableView
{
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    UIView *headerV = [UIView new];
    headerV.backgroundColor = RZ_COLOR(245, 245, 245);
    headerV.height = 15;
    self.tableView.tableHeaderView = headerV;
    
    UIView *footerV = [UIView new];
    footerV.backgroundColor = [UIColor whiteColor];
    footerV.height = 30;
    self.tableView.tableFooterView = footerV;
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([SPLiveHistoryTableViewCell class]) bundle:nil]  forCellReuseIdentifier:NSStringFromClass([SPLiveHistoryTableViewCell class])];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(fetchData)];
}


#pragma mark--

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 125;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SPLiveHistoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SPLiveHistoryTableViewCell class])];
    cell.model = self.dataSource[indexPath.row];
    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SPLiveDeatilTableViewController *detail = [[SPLiveDeatilTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
    detail.act_id = self.dataSource[indexPath.row].hid;
    [self.navigationController pushViewController:detail animated:YES];
}


#pragma mark--fetchData
- (void)fetchData
{
    /**
     http://dev.sharepanzer.com/api/bzapi.php?i=10&c=entry&ctrl=live_merch&ac=member&op=member&m=bz_activities&ta=history_live&token=568e86c8630f7b44f29a23ba211d9f25
     参数 page 第几页 psize 每页多少条 默认5
     
     */
    
    HelenParametersDic *dict = [HelenParametersDic h_dictionary];
    dict[@"ac"] = @"member";
    dict[@"op"] = @"member";
    dict[@"ta"] = @"history_live";
    dict[@"page"] = @(self.pageIndex);
    dict[@"psize"] = @(10);
    dict[@"token"] = [RZ_USERDEFAULTES objectForKey:BUSSION_INFO][@"token"];
    
    [HelenHttpClient requestWithUrlString:BASE_URL andParam:dict andMethod:@"POST" progress:nil success:^(id responseObject) {
        
        [self.tableView.mj_footer endRefreshing];
        NSDictionary *res = responseObject[@"message"];
        NSInteger code = [res[@"resultCode"] integerValue];
        if(code== 0 || code ==1){
            
            NSArray<SPHistoryLiveModel *> *items = [SPHistoryLiveModel mj_objectArrayWithKeyValuesArray:res[@"data"]];
            if(self.pageIndex == 1){
                [self.dataSource removeAllObjects];
            }
            [self.dataSource addObjectsFromArray:items];
            [self.tableView reloadData];
            self.pageIndex++;
            
        }else{
            [WToast showWithText:res[@"resultMessage"]];
            [self.tableView reloadData];
        }
        
    } andFailBlock:^(NSInteger ErrorCode, NSError *ErrorString) {
        [self.tableView.mj_footer endRefreshing];
        [self.tableView reloadData];
    }];
}

@end
